package edu.uoc.tdp.pac4.database;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DiskManager {
	
	private final String propertiesFile = "properties/configuration.properties";
	String driverClassName = "org.postgresql.Driver";
	String connectionUrl;// = "jdbc:postgresql:asistencia";
	String dbUser;// = "postgresql";
	String dbPwd;// = "1234";

	private static DiskManager connectionFactory = null;

	private DiskManager() {
		try {
			Class.forName(driverClassName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	
	
	public Connection getConnection() throws SQLException {
		try {
			Properties prop = new Properties();
			prop.load(ClassLoader.getSystemClassLoader().getResourceAsStream(propertiesFile));
			String connectionUrl = prop.getProperty("url");
			String dbUser = prop.getProperty("username");
			String dbPwd = prop.getProperty("password");
			Connection conn = DriverManager.getConnection(connectionUrl, dbUser, dbPwd);
			return conn;
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("no properties found");
		}
		
		
	}

	public static DiskManager getInstance() {
		if (connectionFactory == null) {
			connectionFactory = new DiskManager();
		}
		return connectionFactory;
	}
}